﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PhilipService;

namespace PhilipSupport.Controls.MainControl
{
    public partial class RecordControl : UserControl
    {
        private long ServiceId;
        public delegate void NoteChangeHandler(string note);
        public event NoteChangeHandler NoteChanged;

        public RecordControl(long serviceId, string note, bool showBtn = false)
        {
            InitializeComponent();
            ServiceId = serviceId;
            tb_note.Text = note;
            if (showBtn)
            {
                this.btn_save.Visible = true;
                this.tb_note.ReadOnly = false;
            }
            else
            {
                this.btn_save.Visible = false;
                this.tb_note.ReadOnly = true;
            }
        }

        private void panel_Paint(object sender, PaintEventArgs e)
        {
            var pen = new Pen(Color.LightGray);
            e.Graphics.DrawLine(pen, new Point(0, 0), new Point(panel.Width, 0));
            e.Graphics.DrawLine(pen, new Point(panel.Width - 1, 0), new Point(panel.Width - 1, panel.Height - 1));
            e.Graphics.DrawLine(pen, new Point(0, panel.Height - 1), new Point(panel.Width - 1, panel.Height - 1));
            e.Graphics.DrawLine(pen, new Point(0, panel.Height - 1), new Point(0, 0));
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                var manager = new RestServiceManager();
                manager.AddOrUpdateServiceNote(ServiceId, tb_note.Text);
                this.Invoke(new Action(() =>
                {
                    NoteChanged?.Invoke(tb_note.Text);
                    PhilipContext.supportMain.RemoveControl();
                }));
            });
        }
    }
}
